const MenuUtil = require('../../util/businessUtil/MenuUtil')
const Role = require('../../model/Role')
const errors = require('../../errors')
const LogUtil = require('../../util/baseUtil/LogUtil')
const moment = require('moment')

module.exports = {

    index_get: async(req, res, next) => {

        let data = req.body.array_object

        let data2 = data.map(value => {
            return { id: value.id + 1 }
        })

        return res.ok()
    },
    index_post: async(req, res, next) => {
        return res.json({
            uuid: '12345',
            token: '123456',
            name: 'test',
            code: 200
        })
    },
    getMenuListDefault: async(req, res, next) => {
        console.log('before')
        try {
            let menu = MenuUtil('default')
            res.ok('获取默认菜单成功', menu)
        } catch (e) {
            next(e)
        }


    },

    getMenuListAfterLogin: async(req, res, next) => {
        try {

            if (!req.session.user) {
                return next(new errors.ValidationError("用户尚未登录"))
            }
            let role = req.session.user.role
            if (!role || role.length === 0) {
                let menu = MenuUtil('default')
                return res.ok('获取默认菜单成功', menu)
            } else {
                Role.find({})
                    .where('_id').in(role)
                    .exec(function(err, result) {
                        result = result.map(value => value.name)
                        let menu = MenuUtil(result)
                        return res.ok('获取菜单成功', menu)
                    })
            }
        } catch (e) {
            next(e)
        }

    }
}